package com.ticket.sass.admin.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ticket.sass.admin.business.TenantBusiness;
import com.ticket.sass.admin.manager.ThreadLocalIdInputHolderManager;
import com.ticket.sass.admin.dto.input.tenants.TransactionInput;
import com.ticket.sass.admin.dto.output.TenantInfoOutput;
import com.ticket.sass.admin.dto.output.tenants.TransactionDataOutput;
import com.ticket.sass.admin.dto.output.tenants.TransactionOutput;
import com.ticket.sass.admin.handler.IdentityInput;
import com.ticket.sass.admin.model.LabModel;
import com.ticket.sass.admin.model.PageModel;
import com.ticket.sass.admin.service.impl.*;
import com.ticket.sass.admin.util.ModelUtil;
import com.ticket.sass.common.entity.provider.TenantEntity;
import com.ticket.sass.common.entity.tenant.TenantFundFlowEntity;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ywb
 * @date 2024-03-19 14:57
 */
@Slf4j
@Service
@AllArgsConstructor
public class TenantBusinessImpl implements TenantBusiness {
    final ModelUtil modelUtil;
    final CinemaServiceImpl cinemaService;
    final TenantAccountServiceImpl accountService;
    final TenantServiceImpl tenantService;
    final TenantFundFlowServiceImpl tenantFundFlowService;

    
    @Override
    public TransactionDataOutput info(TransactionInput input) {
        TransactionDataOutput output = new TransactionDataOutput();
        output.setBalance(accountService.getBalance());
        
        IPage<TenantFundFlowEntity> page = tenantFundFlowService.iPage(input);
        List<TransactionOutput> outputs = modelUtil.listToList(
            page.getRecords(),
            new TypeToken<List<TransactionOutput>>(){}.getType()
        );
        
        output.setPage(new PageModel<>(page.getTotal(), outputs));
        return output;
    }
    
    @Override
    public TenantInfoOutput cinemaList() {
        IdentityInput identityInput = ThreadLocalIdInputHolderManager.getValue();
        Long tenantId = identityInput.getTenantId();
        TenantEntity byId = tenantService.getById(tenantId);
        
        List<LabModel> labModels = cinemaService.getLabModels(new QueryWrapper<>());
        TenantInfoOutput output = new TenantInfoOutput();
        output.setName(byId.getName());
        output.setCinemas(labModels);
        return output;
    }
}
